package com.xisoft.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 集合对象的一些助手工具类
 *
 * @version 1.0<br>
 * @qualiFild nc.pub.itf.tools.pub.CollectionUtil.java<br>
 * @author：licheng<br>
 * @date Created on 2019-8-12<br>
 */
public final class CollectionUtil implements IPubCons, Cloneable {

    /**
     * *********************************************************** <br>
     * 说明：mapList转json 内部类，建议使用这个方法<br>
     *
     * @param mapList
     * @return <br>
     * @throws Exception
     * @String <br>
     * @methods pers.bc.utils.pub.CollectionUtil#map2JosnStr <br>
     * @author LiBencheng <br>
     * @date Created on 2020-6-30 <br>
     * @time 上午9:00:06 <br>
     * @version 1.0 <br>
     * ************************************************************ <br>
     * @see <br>
     */
    public static String map2Josn(List<?> mapList) throws Exception {
        if (PubEnvUtil.getSize(mapList) < 1) {
            return EMPTY;
        }
        StringBuilder strb = new StringBuilder(LEFT_SQ_BRACKET);
        for (Object obj : mapList) {
            if (PubEnvUtil.isEmptyObj(obj)) {
                strb.append(obj).append(COMMA).append(CRLF);
            } else {
                if (obj instanceof Map) {
                    strb.append(map2Josn((Map<?, ?>) obj)).append(COMMA).append(CRLF);
                } else {
                    strb.append(obj).append(COMMA).append(CRLF);
                }
            }
        }

        return deleteStr(strb, 3).append(RIGHT_SQ_BRACKET).toString();
    }

    /**
     * *********************************************************** <br>
     * 说明： map转json,内部类，建议使用这个方法<br>
     *
     * @param map
     * @return <br>
     * @throws Exception
     * @String <br>
     * @methods pers.bc.utils.pub.CollectionUtil#map2JosnStr <br>
     * @author LiBencheng <br>
     * @date Created on 2020-6-30 <br>
     * @time 上午8:59:56 <br>
     * @version 1.0 <br>
     * ************************************************************ <br>
     * @see <br>
     */
    public static String map2Josn(Map<?, ?> map) throws Exception {

        if (PubEnvUtil.getSize(map) < 1) {
            return EMPTY;
        }
        StringBuilder sb = new StringBuilder(LEFT_BRACE);
        for (Object key : map.keySet()) {
            Object obj = map.get(key);
            if (PubEnvUtil.isEmptyObj(obj)) {
                sb.append(QUOTE)
                        .append(key)
                        .append(QUOTE)
                        .append(COLON)
                        .append(QUOTE)
                        .append(StringUtil.valueOfEmpty(obj))
                        .append(QUOTE)
                        .append(COMMA)
                        .append(CRLF);
            } else {
                if (obj instanceof Map) {
                    sb.append(QUOTE)
                            .append(key)
                            .append(QUOTE)
                            .append(COLON)
                            .append(map2Josn((Map<?, ?>) obj))
                            .append(COMMA)
                            .append(CRLF);
                } else if (obj instanceof List) {
                    sb.append(QUOTE)
                            .append(key)
                            .append(QUOTE)
                            .append(COLON)
                            .append(map2Josn((List<?>) obj))
                            .append(COMMA)
                            .append(CRLF);
                } else if (String.valueOf(obj).contains(LEFT_BRACE) || String.valueOf(obj).contains(LEFT_SQ_BRACKET)) {
                    sb.append(QUOTE).append(key).append(QUOTE).append(COLON).append(obj).append(COMMA).append(CRLF);
                } else {
                    sb.append(QUOTE)
                            .append(key)
                            .append(QUOTE)
                            .append(COLON)
                            .append(QUOTE)
                            .append(obj)
                            .append(QUOTE)
                            .append(COMMA)
                            .append(CRLF);
                }
            }
        }

        return deleteStr(sb, 3).append(RIGHT_BRACE).toString();
    }

    public static Map<String, Object> transBean2Map(Object obj) throws Exception {
        if (null == obj) {
            return null;
        }
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            // 过滤class属性
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                map.put(key, value);
            }
        }
        return map;
    }

    public static StringBuilder deleteStr(StringBuilder sbStr, int i) {
        return sbStr.delete(sbStr.length() - i, sbStr.length());
    }
}
